home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC]
/
NeXTSTEP 3.3 Dev Intel.iso
/
usr
/
sybase
/
sample
/
dblibrary
/
example9.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-04-22
|
7KB
|
284 lines
/*
* example9.c
*
* This example illustrates the use of UNIX-specific routines
* to give applications greater control over the I/O blocking
* behavior of DB-Library.
*/
/* The variables SYSV, BSD42, AIX, HP9000_300
* and HP9000_800 are defined in sybfront.h.
*/
#include <sybfront.h>
#include <sybdb.h>
#if BSD42 /* System BSD 4.2 */
#define POLL_CALL 0
#include <sys/time.h>
#endif
#if SYSV && !(HP9000_300 || HP9000_800 || AIX) /* System V (not HP-UX) */
#define POLL_CALL 1
#include <stropts.h>
#include <poll.h>
#define getdtablesize() (_NFILE)
#endif
#if SYSV && (HP9000_300 || HP9000_800) /* System V (HP-UX) */
#define POLL_CALL 0
#include <stdio.h>
#include <sys/time.h>
#define getdtablesize() (_NFILE)
#endif
#if AIX /* System AIX */
#define POLL_CALL 0
#include <sys/select.h>
#endif
#define DATELEN 26
/* Forward declarations of the error-handler, message-handler, and
* utility routines.
*/
void do_something_useful();
int err_handler();
int msg_handler();
main(argc, argv)
int argc;
char *argv[];
{
DBPROCESS *dbproc; /* Our connection with the SQL Server */
LOGINREC *login; /* Our login information. */
/* These are the variables used to store the returning data. */
DBCHAR datebuf[DATELEN];
DBCHAR namebuf[MAXNAME+1]; /* MAXNAME is defined in
* sybdb.h as the maximum
* length for names of
* database objects, such
* as tables, columns, and
* procedures.
*/
/* Initialize DB-Library. */
if (dbinit() == FAIL)
exit(ERREXIT);
/* Install the user-supplied error-handling and message-handling
* routines. They are defined at the bottom of this source file.
*/
dberrhandle(err_handler);
dbmsghandle(msg_handler);
login = dblogin();
DBSETLPWD(login, "server_password");
DBSETLAPP(login, "example9");
dbproc = dbopen(login, NULL);
/*
* We are going to retrieve some information, from a table
* named "sysdatabases", regarding names and creation dates
* of databases.
*/
/* First, put the command into the command buffer. */
dbcmd(dbproc, "select name, crdate from master..sysdatabases");
/* Now it's time to send the command to the SQL Server. If the
* network isn't yet ready for some reason, we can do something
* useful while we're waiting.
*
* In a real application, the program may use this opportunity to
* donate time to a concurrently running task or to monitor some
* other input stream.
*
* In this case, we'll just print out a message, stating that
* there's time available.
*/
while (ioselect(DBIOWDESC(dbproc),FALSE) == 0)
{
do_something_useful("dbsqlsend");
}
/* Now, the network is available for writing. */
dbsqlsend(dbproc);
/* Now it's time to receive a response from the SQL Server. If the
* response isn't yet ready for some reason, we can again do something
* useful while we're waiting.
*/
while
(!(DBRBUF(dbproc))
&& (ioselect(DBIORDESC(dbproc),TRUE) == 0))
{
do_something_useful("dbsqlok");
}
/* Now, the response from the SQL Server is available. */
dbsqlok(dbproc);
/* From this point on, we will be alternately waiting for and
* receiving information from the SQL Server. In each case,
* we will try to do something useful whenever the information
* is not yet available.
*/
while
(!(DBRBUF(dbproc))
&& (ioselect(DBIORDESC(dbproc),TRUE) == 0))
{
do_something_useful("dbresults");
}
dbresults(dbproc);
printf("Database information:\n\n");
while
(!(DBRBUF(dbproc))
&& (ioselect(DBIORDESC(dbproc),TRUE) == 0))
{
do_something_useful("dbnextrow");
}
while (dbnextrow(dbproc) != NO_MORE_ROWS)
{
strncpy
(namebuf, (char *)dbdata(dbproc, 1), dbdatlen(dbproc, 1));
namebuf[dbdatlen(dbproc, 1)] = '\0';
dbconvert(dbproc, SYBDATETIME, (dbdata(dbproc, 2)), -1, SYBCHAR,
datebuf, -1);
printf("\t%s\t%s\n", namebuf, datebuf);
}
dbexit();
exit(STDEXIT);
}
void do_something_useful(str)
char *str;
{
printf("Free time available while waiting to call \"%s\".\n", str);
}
/* ioselect(fd, readFlag)
* informs the caller whether or not the file descriptor fd is ready
* for reading or writing.
* arguments:
* fd : file descriptor to be checked
* readFlag : TRUE - for reading
* FALSE - for writing
* returns:
* 0 : if fd is not ready for reading or writing.
* 1 : if fd is ready for reading or writing.
* Two implementations are provided: one using the System V poll call,
* and the other one using the BSD 4.2 select call.
*/
#if POLL_CALL
/* function definition using System V poll call */
int ioselect(fd,readFlag)
int fd;
DBBOOL readFlag;
{
struct pollfd pollFds;
pollFds.fd = fd;
/* Set the requested event(s), depending on the boolean readFlag. */
pollFds.events = ((readFlag == TRUE) ? (POLLIN | POLLPRI) : POLLOUT);
return( poll (&pollFds, (unsigned long) 1, (int) 0));
}
#else
/* function definition using BSD select() call */
int ioselect(fd,readFlag)
int fd;
DBBOOL readFlag;
{
int readMask, writeMask;
struct timeval timeout;
int returnValue;
/* Set for no wait time. */
timeout.tv_sec = (unsigned long) 0;
timeout.tv_usec = (long) 0;
if(readFlag == TRUE)
{
/* for reading */
readMask = 1 << fd;
returnValue = select ((int) fd+1, &readMask, (int *)NULL,
(int *)NULL, &timeout);
}
else
{
/* for writing */
writeMask = 1 << fd;
returnValue = select ((int) fd+1, (int *)NULL, &writeMask,
(int *)NULL, &timeout);
}
return(returnValue);
}
#endif
int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
DBPROCESS *dbproc;
int severity;
int dberr;
int oserr;
char *dberrstr;
char *oserrstr;
{
if ((dbproc == NULL) || (DBDEAD(dbproc)))
return(INT_EXIT);
else
{
printf("DB-Library error:\n\t%s\n", dberrstr);
if (oserr != DBNOERR)
printf("Operating-system error:\n\t%s\n", oserrstr);
return(INT_CANCEL);
}
}
int msg_handler(dbproc, msgno, msgstate, severity, msgtext,
srvname, procname, line)
DBPROCESS *dbproc;
DBINT msgno;
int msgstate;
int severity;
char *msgtext;
char *srvname;
char *procname;
DBUSMALLINT line;
{
printf ("Msg %ld, Level %d, State %d\n",
msgno, severity, msgstate);
if (strlen(srvname) > 0)
printf ("Server '%s', ", srvname);
if (strlen(procname) > 0)
printf ("Procedure '%s', ", procname);
if (line > 0)
printf ("Line %d", line);
printf("\n\t%s\n", msgtext);
return(0);
}